﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using Zj.DDDSample.Domain.Core;
using Zj.DDDSample.Repositories.EntityFrameworkCore.Models;

namespace Zj.DDDSample.Repositories.EntityFrameworkCore
{
    /// <summary>
    /// project init seed data
    /// </summary>
    public class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = serviceProvider.GetRequiredService<ProjectDbContext>())
            {
                if (context.Project.Any())
                {
                    return;   // 已经初始化过数据，直接返回
                }
                //Add projects.
                List<Project> pList = new List<Project>() {
                      new Project
                   {
                       Name="凤凰新华ERP"

                   },
                    new Project
                    {
                       Name="凤凰新华Crawer"
                    }
                };
                context.Project.AddRange(pList);
                //Add items.
                var iList = new List<Item>() {
                          new Item
                   {
                               Title="设备支出5600*4",
                               ProjectId=pList.First().Id,
                   },
                   new Item
                   {
                        Title="设备收入5600*5",
                               ProjectId=pList.First().Id,
                   },
                          new Item
                   {
                               Title="设备支出5600*6",
                               ProjectId=pList.Last().Id,
                   },
                   new Item
                   {
                        Title="设备收入5600*7",
                               ProjectId=pList.Last().Id,
                   }
                };
                context.Item.AddRange(iList);
                context.SaveChanges();
            }
        }
    }
}
